package com.eco
{
	import com.eco.*;
	import com.framework.*;
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.utils.*;
	
	//敌人基类，继承自刚体小球类
	//是各种敌人类的基类
	public class Enemy extends RigidBall
	{	
		protected var mcTexture:MovieClip;
		protected var ftFighter:Fighter;//卫士类的引用，用于获取卫士类的一些参数
		
		public var iType:int;//敌人的类型
		
		//以下声明各种类型的敌人
		static public const ENEMY_AXE:int = 0;
		static public const ENEMY_ESAW:int = 1;
		static public const ENEMY_CRUDEOIL:int = 2;
		static public const ENEMY_DARKCRUDEOIL:int = 3;
		static public const ENEMY_HOOK:int = 4;
		static public const ENEMY_SPEAR:int = 5;
		
		public var iState:int;//敌人的状态
		
		//以下声明各种状态
		static public const STATE_IDLE:int = 0;
		static public const STATE_ATTACK:int = 1;
		static public const STATE_SKILL:int = 2;
		static public const STATE_DEAD:int = 3;
		static public const STATE_END:int = 4;
		
		static public const COLLISION_WIN:int = 5;
		static public const COLLISION_LOSE:int = 6;
		
		protected var iFreezedFrames:int;//敌人撞击卫士后给卫士的硬直时间
		protected var iAttackPower:int;//敌人攻击时的能量，实际上是敌人攻击时的最大初速度
		protected var iAttackPossibility:int;//攻击的可能性，进入攻击状态转换的依据，真实概率为千分之iAttackPossibility
		protected var iSkillLastingTime:int;//技能持续时间（帧数）
		protected var iCurTime:int;//当前已经走过的时间（帧数）
		public var bCollidable:Boolean;//是否可以碰撞，Rigid Ball中的可以碰撞是指物理层，这里的可以碰撞是指技能
		
		public function Enemy( mass:Number = 10, r:int = 10,type:int = 0 )
		{
			//基类构造函数，建立刚体
			super(mass,r);
			
			iType = type;
			iState = STATE_IDLE;
			CreateEnemyMovieClip(iType);

			ftFighter = null;
			
			iFreezedFrames = 24;
			iAttackPower = 5;
			iAttackPossibility = 5;
			
			iSkillLastingTime = 0;
			iCurTime = 0;
			
			bCollidable = true;
			
			CreateSoundEvent( CustomEventSound.PLAY_SOUND, SoundInfo.EFF_ENEMY_APPEAR);
			
		}
		
		private function CreateEnemyMovieClip(type:int):void
		{
			switch (type)
			{
				case ENEMY_AXE:
				{
					mcTexture = new resEnemyAxe();
					break;
				}
				case ENEMY_ESAW:
				{
					mcTexture = new resEnemyESaw();
					break;
				}
				case ENEMY_CRUDEOIL:
				{
					mcTexture = new resEnemyCrudeOil();
					break;
				}
				case ENEMY_DARKCRUDEOIL:
				{
					mcTexture = new resEnemyBlackCrudeOil();
					break;
				}
				case ENEMY_HOOK:
				{
					mcTexture = new resEnemyHook();
					break;
				}
				case ENEMY_SPEAR:
				{
					mcTexture = new resEnemySpear();
					break;
				}
			}
			mcTexture.scaleX = 0.8;
			mcTexture.scaleY = 0.8;
			this.SetState( Enemy.STATE_IDLE );
			addChild(mcTexture);
		}
		
		override protected function Rotate():void
		{
			if( this.v2dSpeed.getLength() <= 0.5 )
			{
				var _rotation:Number = 90-Math.atan2( ftFighter.x - x, ftFighter.y - y )/Math.PI*180;
				TweenLite.to(this, 0.03, {rotation:_rotation, ease:Elastic.easeInOut});
			}
			else
				super.Rotate();
		}
		
		public function Update():void
		{	
			Move();
			Friction();//执行摩擦模拟模块
			
			switch( iState )
			{
				case Enemy.STATE_IDLE:
				{
					Rotate();
					
					if( Math.random()<= iAttackPossibility/1000 )
					{
						this.SetState( Enemy.STATE_ATTACK );
					}
					
					break;
				}
				case Enemy.STATE_ATTACK:
				{
					Rotate();
					
					//速度一旦为0就结束攻击，进入闲置状态
					if( v2dSpeed.getLength() == 0 )
					{
						this.SetState( Enemy.STATE_IDLE );
					}
					
					break;
				}
				case Enemy.STATE_SKILL:
				{
					iCurTime ++;
					if( iCurTime >= iSkillLastingTime )
					{
						iCurTime = 0;
						EndSkill();
						this.SetState( Enemy.STATE_IDLE );
					}
					break;
				}
				case Enemy.STATE_DEAD:
				{
					//播放死亡的动画，动画结束后跳转到结束状态
					if( mcTexture.currentFrame == mcTexture.totalFrames )
					{
						this.SetState( Enemy.STATE_END );
					}
					break;
				}
				case Enemy.STATE_END:
				{
					break;
				}
			}
		}
		
		public function SetFighter(fighter:Fighter):void
		{
			ftFighter = fighter;
		}
		
		protected function CreateSoundEvent(type:String, name:String ):void
		{
			var evt:CustomEventSound = new CustomEventSound( type, name,false,1,0,0.2);
			dispatchEvent(evt);
		}
		
		public function SetState( state:int ):void
		{
			switch( state )
			{
				case Enemy.STATE_IDLE:
				{
					iState = Enemy.STATE_IDLE;
					mcTexture.gotoAndStop("IDLE");
					break;
				}
				case Enemy.STATE_ATTACK:
				{
					iState = Enemy.STATE_ATTACK;
					//mcTexture.gotoAndPlay("ATTACK");
					//CreateSoundEvent( CustomEventSound.PLAY_SOUND, SoundInfo.EFF_ENEMY_ATTACK);
					Attack();
					break;
				}
				case Enemy.STATE_SKILL:
				{
					iState = Enemy.STATE_SKILL;
					StartSkill();
					break;
				}
				case Enemy.STATE_DEAD:
				{
					if( iState != STATE_DEAD && iState != STATE_END)
					{
						iState = Enemy.STATE_DEAD;
						mcTexture.gotoAndPlay("DEAD");
						CreateSoundEvent( CustomEventSound.PLAY_SOUND, SoundInfo.EFF_ENEMY_DIE);
					}
					break;
				}
				case Enemy.STATE_END:
				{
					iState = Enemy.STATE_END;
					break;
				}
				case Enemy.COLLISION_WIN:
				{
					BeatFighter();
					break;
				}
				case Enemy.COLLISION_LOSE:
				{
					BeBeaten();
					break;
				}	
			}
		}
		
		protected function MoveControl():void
		{
			if( !ftFighter )
				return;
			
			var dis:MyVector2D = new MyVector2D(ftFighter.x - x, ftFighter.y - y);
			dis.setLength( iAttackPower );
			v2dSpeed.plus( dis );
		}
		
		protected function Attack():void
		{
			if( !ftFighter )
				return;
			
			MoveControl();
		}
		
		protected function StartSkill():void
		{
			;
		}
		
		protected function EndSkill():void
		{
			;
		}
		
		protected function BeatFighter():void
		{
			ftFighter.Freezed( iFreezedFrames );
		}
		
		protected function BeBeaten():void
		{
			mcTexture.gotoAndPlay("BEBEATED");
		}
		
		public function Pause():void
		{
			mcTexture.stop();
		}
		
		public function Continue():void
		{
			mcTexture.play();
		}
	}
}